package cn.shenlong.portal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Page;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Company;
import cn.shenlong.portal.domain.Store;
import cn.shenlong.portal.mapper.CompanyMapper;
import cn.shenlong.portal.service.CompanyService;
import cn.shenlong.portal.service.StoreService;

@Service("CompanyService")
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyMapper CompanyMapper;
	
	@Autowired
	private StoreService storeService;
	
	@Override
	@Transactional
	public void save(Company company,long companyId) {
		CompanyMapper.saveEntity(company);
	}

	@Override
	public Pagination findCompany(Company company, Map<String, Object> otherParm,Integer pageSize, Integer pageNo) {
		Pagination p = new Pagination();
		List<Company> contentList= CompanyMapper.findPageListByCriteria(initcompanyParm(company,otherParm), initRowBounds(pageSize, pageNo, p));
		Long count = CompanyMapper.findCountByCriteria(initcompanyParm(company,otherParm));
		p.setList(contentList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	@Override
	public Page<Company> findPageByParm(Map<String, Object> otherParm, Integer pageNo, Integer pageSize) {
		Page<Company> p = new Page<Company>();
		List<Company> eventList = CompanyMapper.findPageListByCriteria(otherParm, initRowBounds(pageSize, pageNo, p));
		Long count = CompanyMapper.findCountByCriteria(otherParm);
		p.setList(eventList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}
	
	private Map<String,Object> initcompanyParm(Company company,Map<String,Object> otherParm){
		Map<String,Object> parm = new HashMap<String, Object>();
		if(otherParm!=null){
			parm.putAll(otherParm);
		}
		if(company==null){
			return parm;
		}else{
			parm.put("companyId", company.getCompanyId());
			parm.put("companyName", company.getCompanyName());
			parm.put("linkUrl", company.getLinkUrl());
		    parm.put("linkMan", company.getLinkMan());
		    parm.put("address", company.getAddress());
		    parm.put("telephone", company.getTelephone());
		    parm.put("email", company.getEmail());
		    parm.put("description", company.getDescription());
		    parm.put("rank", company.getRank());
		    parm.put("imgUrl", company.getImgUrl());
		    parm.put("isForMember", company.getIsForMember());
		    /*parm.put("isDeleted", company.getIsDeleted());
		    parm.put("createDate", company.getCreateDate());
		    parm.put("createby", company.getCreateby());
		    parm.put("updateDate", company.getUpdateDate());
		    parm.put("updateby", company.getUpdateby());*/
			return parm;
		}

	}
    private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Pagination p) {
        RowBounds rowBounds = null;
        if (pageSize == null || pageNo == null) {
            rowBounds = new RowBounds();
        } else {
            p.setPageNo(pageNo);
            p.setPageSize(pageSize);
            rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
        }
        return rowBounds;
    }

	@Override
	public Company findCompanyByCompanyId(long companyId) {
		return CompanyMapper.findByPK(companyId);
	}

	@Transactional
	@Override
	public Company updateCompany(Company company) {
		int i = CompanyMapper.updateEntity(company);
		if (i < 0) return null;
		company = CompanyMapper.findByPK(company.getCompanyId());

		// 处理商家
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", company.getCompanyId().intValue());
		Page<Store> p = storeService.findPageListByCriteria(1, 1, map);
		Store store = null;
		if (p.getList() != null && !p.getList().isEmpty()) {
			store = (Store) p.getList().get(0);
		}
		
		if (company.getIsForMember() != null && company.getIsForMember() == true) {//是会员专享，商家需要有对应的记录
			if (store == null) {
				store = new Store();
				store.setCompanyId(company.getCompanyId().intValue());
				store.setDataflg(1);
				
				storeService.saveEntity(store);
			}
		} else {//不是会员专享，商家不可有对应记录
			if (store != null) {
				storeService.deleteByPKArray(new Integer[]{ store.getStoreId() });
			}
		}

		return company;
	}

	@Override
	public void deleteCompany(Long[] companyId) {
		CompanyMapper.deleteByPKArray(companyId);
	}

	@Override
	public void deleteByCompanyId(List<Long> CompanyId) {
		CompanyMapper.deleteByPKArray((Long[])CompanyId.toArray());
	}
	
	public void save(Company company){
		CompanyMapper.saveEntity(company);
		
		if (company.getIsForMember() != null && company.getIsForMember() == true) {
			Store store = new Store();
			store.setCompanyId(company.getCompanyId().intValue());
			store.setDataflg(1);
			
			storeService.saveEntity(store);
		}
	}
}
